// 导入model层
const userAttentionAct = require('../model/userAttentionAct.model')

class userAttentionActService {
    /**
     * 创建关注
     */
    async createAttention (attentionData) {
        try {
            const res = await userAttentionAct.create(attentionData)
            return res
        } catch (error) {
            return {
                errorFlag: 'error',
                errorCode: error.parent.code,
                message: error.parent.sqlMessage,
            }
        }
    }

    /**
     * 获取关注列表
     */
    async getAttentionActList ({ user_account }) {
        const res = await userAttentionAct.findAll({
            where: { user_account },
            order: [
                // 将转义 title 并针对有效方向列表进行降序排列
                ['createdAt', 'DESC'],
            ],
        })
        return res
    }

    /**
     * 判断用户是否已经关注
     */
    async isAttention ({ act_id, user_account }) {
        const res = await userAttentionAct.findAndCountAll({ where: { act_id, user_account } })
        return res
    }

    /**
     * 取消关注
     */
    async cancelAttention ({ act_id, user_account }) {
        try {
            const res = await userAttentionAct.destroy({ where: { act_id, user_account } })
            return res
        } catch (error) {
            console.log(error)
        }
    }
}

module.exports = new userAttentionActService()